Overview:
- There are several quantities we observe change their values over a period of time.
- The most common one observed by or felt by all of us is the temperature. It changes every second. Akin to temperature prices of commodities also change based on various factors.
- The results of scientific experiments change every time the experiment is repeated and the results are deemed to be acceptable by the practitioners if they are within a range a values. Changes are everywhere and occur all the time.
- The changes in these variables are often easily understood when expressed in terms of percentage relative to previous value at some point time in the past. News reports often provide percentage change of price for stocks and commodities with respect to their previous day closing prices.
Percentage Change calculation for the data in a pandas series:
- A pandas Series is a one dimensional ndarray combined with the most essential functions for data analysis.
- The function pct_change() of a pandas.series instance calculates the percentage change between the elements - the current element vs the previous element.
Example:
# Example python program that calculates percentage change # of values between pandas.Series elements
import pandas as pds
# Stock price over 7 periods stockPrice = [123.1, 122.4, 123.0, 123.7, 124.5, 124.9, 125.5]
# Create a pandas Series instance percentage_Change = pds.Series(stockPrice); print("Contents of the pandas Series:"); print(stockPrice);
print("Percentage change between the element values of the pandas Series:"); print(percentage_Change); print(type(percentage_Change)); |
Output:
Contents of the pandas Series: [123.1, 122.4, 123.0, 123.7, 124.5, 124.9, 125.5] Percentage change between the element values of the pandas Series: 0 123.1 1 122.4 2 123.0 3 123.7 4 124.5 5 124.9 6 125.5 dtype: float64 <class 'pandas.core.series.Series'> |